package com.thenie.web;


import com.thenie.pojo.Cart;
import com.thenie.pojo.User;
import com.thenie.service.OrderService;
import com.thenie.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class OrderServlet extends BaseServlet {

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        OrderService orderService = new OrderServiceImpl();
        User user=(User)req.getSession().getAttribute("loginUser");

        if(user==null){
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        Cart cart=(Cart)req.getSession().getAttribute("cart");
        String orderId = orderService.createOrder(cart, user.getId());

        cart.clear();
        req.getSession().setAttribute("lastOrderId", orderId);

        resp.sendRedirect(req.getContextPath()+"/pages/cart/checkout.jsp");

    }
}
